//
// Created by 29001 on 2024/11/20.
//

#ifndef HANDWRITTEN_SQLITE_SIMPLE_SQL_COMPILER_H
#define HANDWRITTEN_SQLITE_SIMPLE_SQL_COMPILER_H

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#include <errno.h>
#include <stdint.h>
#include "REPL.h"

/*
 * our new functions return enums indicating success or failure
 */
typedef enum {
    META_COMMAND_SUCCESS,
    META_COMMAND_UNRECOGNIZED_COMMAND,
} MetaCommandResult;

/*
 * our sql statement's type
 */
typedef enum {
    STATEMENT_INSERT,
    STATEMENT_SELECT,
} StatementType;

/*
 * our sql statement
 */
typedef struct {
    StatementType type;
} Statement;


/*
 * indicating our Prepare result
 */
typedef enum {
    PREPARE_SUCCESS,
    PREPARE_UNRECOGNIZED_STATEMENT,
} PrepareResult;

/*
 * Handle the metadata, like .exit, our token must be match metadata first,then match the statement
 */
MetaCommandResult do_meta_command(InputBuffer *inputBuffer) {
    if (strcmp(inputBuffer->buffer, ".exit") == 0) {
        exit(EXIT_SUCCESS);
    } else {
        return META_COMMAND_UNRECOGNIZED_COMMAND;
    }
}

/*
 * Handle our statement(not the metadata),this is our sql compiler
 */
PrepareResult prepare_statement(InputBuffer *input_buffer, Statement *statement) {
    if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
        statement->type = STATEMENT_INSERT;
        return PREPARE_SUCCESS;
    }
    if (strncmp(input_buffer->buffer, "select", 6) == 0) {
        statement->type = STATEMENT_SELECT;
        return PREPARE_SUCCESS;
    }
    return PREPARE_UNRECOGNIZED_STATEMENT;
}

/*
 * here just really execute our statement now!
 */

void execute_statement(Statement *statement) {
    switch (statement->type) {
        case STATEMENT_INSERT:
            printf("This is where we would do an insert.\n");
            break;
        case STATEMENT_SELECT:
            printf("This is where we would do an select.\n");
            break;
    }
}


#endif //HANDWRITTEN_SQLITE_SIMPLE_SQL_COMPILER_H
